package com.example.deferredcontroller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import rx.Observable;

import java.time.Duration;

@RestController
public class DeferredTestController {

    @GetMapping("/deferred")
    public DeferredResult<String> deferredResultTest(){

        DeferredResult<String> deferredResult=new DeferredResult<>(Duration.ofMinutes(5L).toMinutes());
        Observable<String> observable=Observable.just("hello").map(i->{
            try {
                Thread.currentThread().sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return i.toUpperCase();
        });

        observable.subscribe(i->
            deferredResult.setResult(i), deferredResult::setErrorResult
        );

        return deferredResult;


    }


}
